﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.Main_Base
{
    public partial class Sys_Setting_Station_CameraEdit : DevExpress.XtraEditors.XtraForm
    {
        public Sys_Setting_Station_CameraEdit()
        {
            InitializeComponent();
        }
        public LeverString_SysTreeMainList _tl = new LeverString_SysTreeMainList();//分类值

        public string _Select;
        public string _LoadType;
        public string _ipl;
        public string _name;
        public string _ipw;
        public string _port;
        public string _pass;
        public string _user;
        public string _nvr_ipl;
        public string _nvr_ipw;
        public string _nvr_num;
        public string _nvr_pass;
        public string _nvr_user;
        public string _nvr_port;
        public string _id;

        private void cameraEditFrm_Load(object sender, EventArgs e)
        {


            if (_Select == "1")
            {
                rb_class.Checked = false;
                rb_camera.Checked = true;
                if (_LoadType == "Add")
                {
                    lbClass.Text = "分类名称:" + _tl.f_name;
                    bt_Sure.Text = "新  增";
                }

                else if (_LoadType == "Edit")
                {
                    rb_class.Visible = false;
                    readAll();
                    bt_Sure.Text = "修  改";
                }
                else
                {
                    rb_class.Visible = false;
                    readAll();
                    tb_ipl.Enabled = false;
                    tb_ipw.Enabled = false;
                    tb_name.Enabled = false;
                    tb_nvr_ipl.Enabled = false;
                    tb_nvr_ipw.Enabled = false;
                    tb_nvr_num.Enabled = false;
                    tb_nvr_pass.Enabled = false;
                    tb_nvr_port.Enabled = false;
                    tb_nvr_user.Enabled = false;
                    tb_pass.Enabled = false;
                    tb_port.Enabled = false;
                    tb_user.Enabled = false;
                    bt_Sure.Text = "删  除";
                }
            }
            else
            {
                rb_class.Checked = true;
                rb_camera.Checked = false;
                lbClass.Text = "本级分类名称：" + _tl.f_name;

                if (_LoadType == "Add")
                {
                    if (_tl.f_id == "M-1")
                    {
                        rb_camera.Visible = false;
                        lbClass.Text = "上级分类名称：" + _tl.f_name;
                        bt_Sure.Text = "新  增";
                    }
                    else
                    {
                        lbClass.Text = "上级分类名称：" + _tl.f_name;
                        bt_Sure.Text = "新  增";
                    }
                }
                else if (_LoadType == "Edit")
                {
                    rb_camera.Visible = false;
                    tb_class.Text = _tl.f_name;
                    bt_Sure.Text = "修  改";
                }
                else
                {
                    rb_camera.Visible = false;
                    tb_class.Text = _tl.f_name;
                    tb_class.Enabled = false;
                    bt_Sure.Text = "删  除";
                }
            } 
            
        }

        private void readAll()
        {
             tb_name.Text=_name;
            tb_ipl.Text=_ipl;
            tb_ipw.Text=_ipw;
            tb_user.Text=_user;
            tb_pass.Text=_pass;
            tb_port.Text=_port;
            tb_nvr_ipl.Text= _nvr_ipl;
            tb_nvr_ipw.Text=_nvr_ipw;
            tb_nvr_num.Text = _nvr_num;
            tb_nvr_user.Text = _nvr_user;
            tb_nvr_pass.Text=_nvr_pass;
            tb_nvr_port.Text = _nvr_port; 

        }


        private void clearAll()      //清空textbox
        {
            tb_name.Text = "";
            tb_ipl.Text = "";
            tb_ipw.Text = "";
            tb_nvr_ipl.Text = "";
            tb_nvr_ipw.Text = "";
            tb_nvr_num.Text = "";
            tb_nvr_pass.Text = "";
            tb_nvr_user.Text = "";
            tb_pass.Text = "";
            tb_user.Text = "";
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            if (rb_class.Checked)
            {
               string strSQL;
            if (_LoadType == "Add")
            {
                if (_tl.f_id=="M-1")
                {
                    strSQL = "insert into TS_S_B_Station_Camera ( f_name,f_level,f_deep,f_type,f_workerid) Values('" + tb_class.Text.Trim() + "','" + _tl.f_level + "'," + _tl.f_deep + ",'0','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "') "; 
                }
                else
                {
                string _level = "";
                int _deep = -1;
                _level = _tl.f_level + _tl.f_id + ",";
                _deep = Convert.ToInt32(_tl.f_deep) + 1;
                strSQL = "insert into TS_S_B_Station_Camera ( f_name,f_level,f_deep,f_type,f_workerid) Values('" + tb_class.Text.Trim() + "','" + _level + "'," + _deep + ",'0','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "') ";
                }
             }
 
            else if (_LoadType == "Edit")
            {
                strSQL = "update TS_S_B_Station_Camera set [f_name] = '" + tb_class.Text.Trim() + " ' where f_id = " + _tl.f_id+ "";
            }
            else
            {
                strSQL = "update TS_S_B_Station_Camera set f_del= 1 ,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id = " + _tl.f_id + " or f_level like '%," + _tl.f_id + ",%' and f_del=0";
            }
            TS_Win_Class.MySqlCom(strSQL);
            this.DialogResult = DialogResult.OK;  
            }
            else
            {
          string  StrSql;               
            if (_LoadType == "Add")
           {
               if (tb_name.Text.Trim() != "" && tb_ipl.Text.Trim() != "" && tb_ipw.Text.Trim() != "" && tb_user.Text.Trim() != "" && tb_pass.Text.Trim() != "" && tb_port.Text.Trim() != "" && tb_nvr_ipl.Text.Trim() != "" && tb_nvr_ipw.Text.Trim() != "" && tb_nvr_num.Text.Trim() != "" && tb_nvr_user.Text.Trim() != "" && tb_nvr_pass.Text.Trim() != "" && tb_nvr_port.Text.Trim() != "")
               {
                   string _level = "";
                   int _deep = 0;
                   _level = _tl.f_level + _tl.f_id + ",";
                   _deep = Convert.ToInt32(_tl.f_deep) + 1;
                   StrSql = "INSERT INTO TS_S_B_Station_Camera (f_name, f_ip_l, f_ip_w, f_port, f_logname, f_logpass, f_nvr_ip_l, f_nvr_ip_w, f_nvr_port, f_nvr_num, f_nvr_logname, f_nvr_logpass, f_workerid,  f_type, f_level, f_deep) VALUES('" + tb_name.Text.Trim() + "','" + tb_ipl.Text.Trim() + "','" + tb_ipw.Text.Trim() + "','" + tb_port.Text.Trim() + "','" + tb_user.Text.Trim() + "','" + tb_pass.Text.Trim() + "','" + tb_nvr_ipl.Text.Trim() + "','" + tb_nvr_ipw.Text.Trim() + "','" + tb_nvr_port.Text.Trim() + "','" + tb_nvr_num.Text.Trim() + "','" + tb_nvr_user.Text.Trim() + "','" + tb_nvr_pass.Text.Trim() + "'," + 0 + ",'1','" + _level + "'," + _deep + ") ";
                   TS_Win_Class.MySqlCom(StrSql);
                   this.DialogResult = DialogResult.OK;
               }
               else
               {
                   MessageBox.Show("有文本框未输入值！");
               }
            }
               else
                if (_LoadType == "Edit")
                {
                    if (tb_name.Text.Trim() != "" && tb_ipl.Text.Trim() != "" && tb_ipw.Text.Trim() != "" && tb_user.Text.Trim() != "" && tb_pass.Text.Trim() != "" && tb_port.Text.Trim() != "" && tb_nvr_ipl.Text.Trim() != "" && tb_nvr_ipw.Text.Trim() != "" && tb_nvr_num.Text.Trim() != "" && tb_nvr_user.Text.Trim() != "" && tb_nvr_pass.Text.Trim() != "" && tb_nvr_port.Text.Trim() != "")
                    {
                        
                        StrSql = "update TS_S_B_Station_Camera set f_name='" + tb_name.Text.Trim() + "',f_ip_l='" + tb_ipl.Text.Trim() + "',f_ip_w='" + tb_ipw.Text.Trim() + "',f_logname='" + tb_user.Text.Trim() + "',f_logpass='" + tb_pass.Text.Trim() + "',f_port='" + tb_port.Text.Trim() + "',f_nvr_ip_l='" + tb_nvr_ipl.Text.Trim() + "',f_nvr_ip_w='" + tb_nvr_ipw.Text.Trim() + "',f_nvr_num='" + tb_nvr_num.Text.Trim() + "',f_nvr_logname='" + tb_nvr_user.Text.Trim() + "',f_nvr_logpass='" + tb_nvr_pass.Text.Trim() + "',f_nvr_port='" + tb_nvr_port.Text.Trim() + "', f_time=getdate() where f_id=" + _id + "  ";
                        TS_Win_Class.MySqlCom(StrSql);
                        clearAll();
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("有文本框未输入值！");
                    }
                }
                else
                {
                    if (tb_name.Text.Trim() != "" && tb_ipl.Text.Trim() != "" && tb_ipw.Text.Trim() != "" && tb_user.Text.Trim() != "" && tb_pass.Text.Trim() != "" && tb_port.Text.Trim() != "" && tb_nvr_ipl.Text.Trim() != "" && tb_nvr_ipw.Text.Trim() != "" && tb_nvr_num.Text.Trim() != "" && tb_nvr_user.Text.Trim() != "" && tb_nvr_pass.Text.Trim() != "" && tb_nvr_port.Text.Trim() != "")
                    {
                  
                        StrSql = "update TS_S_B_Station_Camera set f_del=1 where f_id=" + _id + "  ";
                        TS_Win_Class.MySqlCom(StrSql);
                        clearAll();
                        this.DialogResult = DialogResult.OK;
                    }
                }
           }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            groupControl1.Visible = false;
            groupControl2.Visible = false;
            label1.Visible = true;
            tb_class.Visible = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            groupControl1.Visible = true;
            groupControl2.Visible = true;
            label1.Visible = false;
            tb_class.Visible = false;
        }


            
            
        } 


    }